//
//  JSFeedbackViewController.m
//  NiubilityProject
//
//  Created by Apple on 2017/12/2.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSFeedbackViewController.h"

@interface JSFeedbackViewController ()<QMUITextViewDelegate>
@property (weak, nonatomic) IBOutlet QMUITextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight;
@property (nonatomic, weak) QMUINavigationButton *confrimBtn;
@end

@implementation JSFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.topSpace.constant = kRealValue(15);
    self.leftSpace.constant = self.rightSpace.constant = kRealValue(25);
    self.textView.placeholder = JSLocalizedString(@"feebackLock");
    self.textViewHeight.constant = kRealValue(136);
    QMUINavigationButton *btn = [QMUINavigationButton buttonWithType:UIButtonTypeCustom];
    self.confrimBtn = btn;
    btn.titleLabel.font = UIFontMake(14);
    [btn setTitleColor:JS_colorWithHex(kColorBlack3) forState:UIControlStateNormal];
    [btn setTitleColor:JS_colorWithHex(kColorGray6) forState:UIControlStateDisabled];
    [btn setTitle:JSLocalizedString(kLocalizableOk) forState:UIControlStateNormal];
    [btn sizeToFit];
   RACSignal *enableSignal = [[self rac_signalForSelector:@selector(textViewDidChange:) fromProtocol:@protocol(QMUITextViewDelegate)] map:^id(id value) {
       btn.enabled = YES;
        QMUITextView *tv = value[0];
        return @(tv.text.length);
    }];
    btn.rac_command = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
        
        return [RACSignal empty];
    }];
   
}
- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
   
    self.navigationItem.rightBarButtonItem = [QMUINavigationButton barButtonItemWithNavigationButton:self.confrimBtn position:QMUINavigationButtonPositionRight target:self action:@selector(confirmClick:)];
     self.confrimBtn.enabled = NO;
}
- (void)confirmClick:(UIButton *)sender {
    [JSNetManager js_courrentProjectRequestWithMouth:@"Feedback" cache:NO ifShowFinishStatu:NO parameters:@{@"content":self.textView.text,@"token":userManager.curUserInfo.token}.mutableCopy successBlock:^(id response) {
        if ([response[@"status"] integerValue] == 200) {
            if ([self.view isShowingOnKeyWindow]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [JSAlertHelper showSucceedHideAfterDelay:2 text:JSLocalizedString(kLocalizableSuccess) inView:nil animation:YES];
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        }
    } failureBlock:nil];
}
@end
